﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void GenerateButton_Click(object sender, EventArgs e)
        {
            //生成随机数的方法
            using (StreamWriter writer = new StreamWriter("Generator.txt"))
            {
                Random random = new Random();
                for (int i = 0; i < 100; i++)
                {
                    int number = random.Next(1000);
                    writer.WriteLine(number);
                }
            }
        }

        private void CaculateButton_Click(object sender, EventArgs e)
        {
            string filePath;
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    filePath = openFileDialog.FileName;
                }
                else
                {
                    return; // 退出方法
                }
            }
            StreamReader reader = new StreamReader(filePath);
            string line;
            List<int> list = new List<int>();
            while ((line=reader.ReadLine())!=null)
            {
                string[] strNumbers = line.Split(' ');
                list.Add(int.Parse(strNumbers[0]));//将数据存入数组
            }

            int max=list.Max();
            int min=list.Min();
            double avg=list.Average();
            int sum=list.Sum();
            string result = "最大值:" + max + "最小值:" + min + "平均值:" + avg + "总和" + sum;
            MessageBox.Show("计算结果为：" + result, "Result");
            reader.Close();
        }
    }
}